package com.aaa.config;

import cn.dev33.satoken.filter.SaServletFilter;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.thymeleaf.dialect.SaTokenDialect;
import com.aaa.guard.MyFilter;
import com.aaa.guard.MyHandler;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    /**
     * SaToken 集成 Thymeleaf
     * @return ???
     */
    @Bean
    public SaTokenDialect getSaTokenDialect() {
        System.out.println("加载 SaToken 方言 thymeleaf");
        return new SaTokenDialect();
    }

    @Bean
    @Lazy
    public SaServletFilter getSaServletFilter() {
        System.out.println("手动注册 Sa-Token 的 Servlet 过滤器");
        return new SaServletFilter();
    }
    // 过滤器
    @Bean
    public FilterRegistrationBean<MyFilter> registerMyFilter() {

        FilterRegistrationBean<MyFilter> registration = new FilterRegistrationBean<>();

        registration.setFilter(new MyFilter());

        registration.addUrlPatterns("/**");

        registration.setName("MyFilter");

        registration.setOrder(1);

        // registration.addInitParameter("paramName", "paramValue");

        return registration;
    }
    // 拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 启用注解鉴权 也可以直接 new SaInterceptor(new MyHandler())
        registry.addInterceptor(new SaInterceptor())
                .addPathPatterns("/**");
//                .excludePathPatterns("/**/*.js","/**/*.css","/user/doLogin","/");
    }
    // 指定静态资源的配置
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }

}
